function GetHttpRequest(){
	var al__httpRequest = null;
	
    if ( window.XMLHttpRequest ) // Gecko
        al__httpRequest = new XMLHttpRequest() ;
    else if ( window.ActiveXObject ) // IE 
        al__httpRequest = new ActiveXObject("Microsoft.XMLHTTP") ;
	
	return al__httpRequest;
}


function IncludeJS(sId, fileUrl, source){
    /*if ( ( source != null ) && ( !document.getElementById( sId ) ) ){ 
        var oHead = document.getElementsByTagName('HEAD').item(0); 
        var oScript = document.createElement( "script" ); 
        oScript.language = "javascript"; 
        oScript.type = "text/javascript"; 
        oScript.id = sId; 
        oScript.defer = true; 
        oScript.text = source; 

        oHead.appendChild( oScript ); 
    }//*/
    try{
    	source = _trimSource(source);
	    if ( window.execScript )
			window.execScript( source );
		else 
			eval.call( window, source );
	}catch(e){
		if(document.all)
			alert(e.description + '\nsource:'+source);
		else
			alert(e.message + '\nsource:'+source);
	}
}

//去除包围js源码的<!--  //-->
function _trimSource(source){
	source = source.replace(/^\s+|\s+$/g, "");
	if(source.indexOf('<!--') == 0){
		source = source.substring(4);
	}
	return source;
}


function IncludeHTML(container, fileUrl, source){
	$$(container).innerHTML = source;
	
	//装载js
	var i, j, k;
	var re = /<script/ig, ree = /<\/script>/ig, res = /src=/ig;
	i = re.exec(source);
	while(i != null){
		i = i.index;
		j = ree.exec(source).index;
		res.lastIndex = i;
		k = res.exec(source);
		if(k != null && k.index<j){
			k = k.index;
			var c = source.substr(k+4, 1);
			i = k + 5;
			j = source.indexOf(c, i);
			var jsSrc = source.substring(i, j);
			if(jsSrc.length > 0 && !jsSrc.match(/alpha\.js([\?\.]|$)/))
				loadJS(jsSrc, true);
		}else{
			k = source.indexOf('>', i) + 1;
			var ss = source.substring(k, j);
			if(ss.length > 0){
				IncludeJS('js_'+new Date().getTime(), '', ss);
			}
		}
		i = re.exec(source);
	}
}


function AjaxPage(sId, url, cbF){
    var oXmlHttp = GetHttpRequest() ;

    var onreadystatechange = function(){ 
        if ( oXmlHttp.readyState == 4 ){
            if ( oXmlHttp.status == 200 || oXmlHttp.status == 304 ){
                cbF( sId, url, oXmlHttp.responseText );
            }else{ 
                alert( 'XML request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')' ) ; 
            } 
        } 
    } 

	var p = window.ALPHA_DEBUG ? (((url.indexOf('?')>-1) ? '&' : '?') + 'alpha_t='+new Date().getTime()) : '';
    oXmlHttp.open('GET', url+p, false); 
    oXmlHttp.send(null);
    onreadystatechange();
    oXmlHttp = null;
}


//-------------------------------------------------------------
//获得当前应用根路径。只在当前页面内有效。
function getBaseURI(){
	if(this.baseUri){
		return this.baseUri;
	}
	var scripts = document.getElementsByTagName("script");
	var rePkg = /alpha\.js([\?\.]|$)/i;
	for(var i=0; i<scripts.length; ++i){
		var src = scripts[i].getAttribute("src");
		if(!src) { continue; }
		var m = src.match(rePkg);
		if(m){
			this.baseUri = src.substring(0, m.index) + '../';
			break;
		}
	}
	return this.baseUri;
}

function getLibURI(){
	return getBaseURI() + "Global/";
}


//-----------------------------------------------------------
//动态装载js代码。
//path:js文件路径，reload:是否强制重新装载，默认为false，装载过之后即使再调用也不会再装载。
function loadJS(path, reload){
	if(this[path] && !reload)
		return;
	/*
	if(!window.$){
		AjaxPage('js_'+new Date().getTime(), path, IncludeJS);
	}else{
		$.ajax({
		   type: "GET",
		   url: path,
		   dataType: "script",
		   async: false
		 });
	}//*/
	AjaxPage('js_'+new Date().getTime(), path, IncludeJS);
	this[path] = 1;
}


//装载系统的js文件。jsName:文件名，相对于系统库目录路径，如：window.js, private/alpha-init.js。
function loadSysJS(jsName, reload){
	loadJS(getLibURI() + jsName, reload);
}


//------------------------------------------------------------
//装载css文件。path:css文件路径。
function loadCSS(path, reload){
	if(this[path] && !reload)
		return;
		
	if(isIE()){
		document.createStyleSheet(path);
	}else{
		var style = document.createElement('link'); 
		style.href = path; 
		style.rel = 'stylesheet'; 
		style.type = 'text/css';
		document.getElementsByTagName('HEAD').item(0).appendChild(style);
	}
	
	this[path] = 1;
}


//装载系统CSS。cssName:css文件名，不带路径，如：base.css。style：风格，默认为'default'。
function loadSysCSS(cssName, style, reload){
	if(!style)style='default';
	loadCSS(getLibURI()+style+"/css/"+cssName);
}


function ALPHA_EMPTY_METHOD(){}

//=====================
var ALPHA_DEBUG = true;
loadSysJS("private/alpha-init.js");
